home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / file-tra / wu-ftpd-.000 / wu-ftpd- / wu-ftpd-2.4-fixed / support / getusershell.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-01  |  3.0 KB  |  129 lines

  1. /*
  2.  * Copyright (c) 1985 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that: (1) source distributions retain this entire copyright
  7.  * notice and comment, and (2) distributions including binaries display
  8.  * the following acknowledgement:  ``This product includes software
  9.  * developed by the University of California, Berkeley and its contributors''
  10.  * in the documentation or other materials provided with the distribution
  11.  * and in all advertising materials mentioning features or use of this
  12.  * software. Neither the name of the University nor the names of its
  13.  * contributors may be used to endorse or promote products derived
  14.  * from this software without specific prior written permission.
  15.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  16.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  17.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  18.  */
  19.  
  20. #if defined(LIBC_SCCS) && !defined(lint)
  21. static char sccsid[] = "@(#)getusershell.c    5.6 (Berkeley) 6/1/90";
  22.  
  23. #endif /* LIBC_SCCS and not lint */
  24.  
  25. #include "../src/config.h"
  26. #include <sys/types.h>
  27. #include <sys/param.h>
  28. #include <sys/file.h>
  29. #include <sys/stat.h>
  30. #include <stdlib.h>
  31. #include <ctype.h>
  32. #include <stdio.h>
  33.  
  34. #define SHELLS "/etc/shells"
  35.  
  36. /*
  37.  * Do not add local shells here.  They should be added in /etc/shells
  38.  */
  39. static char *okshells[] =
  40. {"/bin/sh", "/bin/csh", 0};
  41.  
  42. static char **shells,
  43.  *strings;
  44. static char **curshell = NULL;
  45. static char **initshells();
  46.  
  47. /*
  48.  * Get a list of shells from SHELLS, if it exists.
  49.  */
  50. char *
  51. getusershell(void)
  52. {
  53.     char *ret;
  54.  
  55.     if (curshell == NULL)
  56.         curshell = initshells();
  57.     ret = *curshell;
  58.     if (ret != NULL)
  59.         curshell++;
  60.     return (ret);
  61. }
  62.  
  63. void
  64. endusershell(void)
  65. {
  66.     if (shells != NULL)
  67.         free((char *) shells);
  68.     shells = NULL;
  69.     if (strings != NULL)
  70.         free(strings);
  71.     strings = NULL;
  72.     curshell = NULL;
  73. }
  74.  
  75. void
  76. setusershell(void)
  77. {
  78.     curshell = initshells();
  79. }
  80.  
  81. static char **
  82. initshells(void)
  83. {
  84.     register char **sp,
  85.      *cp;
  86.     register FILE *fp;
  87.     struct stat statb;
  88.  
  89.     if (shells != NULL)
  90.         free((char *) shells);
  91.     shells = NULL;
  92.     if (strings != NULL)
  93.         free(strings);
  94.     strings = NULL;
  95.     if ((fp = fopen(SHELLS, "r")) == (FILE *) 0)
  96.         return (okshells);
  97.     if (fstat(fileno(fp), &statb) == -1) {
  98.         (void) fclose(fp);
  99.         return (okshells);
  100.     }
  101.     if ((strings = (char *) malloc((unsigned) statb.st_size + 1)) == NULL) {
  102.         (void) fclose(fp);
  103.         return (okshells);
  104.     }
  105.     shells = (char **) calloc((unsigned) statb.st_size / 3, sizeof(char *));
  106.  
  107.     if (shells == NULL) {
  108.         (void) fclose(fp);
  109.         free(strings);
  110.         strings = NULL;
  111.         return (okshells);
  112.     }
  113.     sp = shells;
  114.     cp = strings;
  115.     while (fgets(cp, MAXPATHLEN + 1, fp) != NULL) {
  116.         while (*cp != '#' && *cp != '/' && *cp != '\0')
  117.             cp++;
  118.         if (*cp == '#' || *cp == '\0')
  119.             continue;
  120.         *sp++ = cp;
  121.         while (!isspace(*cp) && *cp != '#' && *cp != '\0')
  122.             cp++;
  123.         *cp++ = '\0';
  124.     }
  125.     *sp = (char *) 0;
  126.     (void) fclose(fp);
  127.     return (shells);
  128. }
  129.